/*
 * Set.c
 *
 *  Created on: Nov 1, 2014
 *      Author: Ahmed Kamel
 */

#include "Set.h"

void * add (void * _set, const void * _element)
{
	struct Set * set = _set;
	struct Object * element = (void *) _element;

	assert(set);
	assert(element);

	if (! element -> in)
		element -> in = set;
	else
		assert(element -> in == set);

	++ element -> count, ++ set -> count;

	return element;
}

void * find (const void * _set, const void * _element)
{
	const struct Object * element = _element;

	assert(_set);
	assert(element);

	return element -> in == _set ? (void *) element : 0;
}

int contains (const void * _set, const void * _element)
{
	return find(_set, _element) != 0;
}

void * drop (void * _set, const void * _element)
{
	struct Set * set = _set;
	struct Object * element = find(set, _element);

	if (element)
	{
		if (-- element -> count == 0)
			element -> in = 0;
		-- set -> count;
	}
	return element;
}

unsigned count (const void * _set)
{
	const struct Set * set = _set;

	assert(set);
	return set -> count;
}

int action (void * object, va_list ap) {
	return 1;
}

int apply (const void * set, int (* action) (void * object, va_list ap), ...)
{
	va_list args;
	va_start(args, action);

	if (args == 0)
		return 0;

	va_end(args);

	return 1;
}
